package com.example.piece.ui.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.example.base.entity.Rest
import com.example.base.ext.getViewModel
import com.example.base.ext.navigationTo
import com.example.base.ui.fragment.BaseFragment
import com.example.base.vm.onError
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.FragmentZPVideoBinding
import com.example.piece.entity.AttentionEntity
import com.example.piece.entity.JokesVideoLikeListEntity
import com.example.piece.ui.adapter.HomeRecommendationAdapter
import com.example.piece.ui.adapter.ZPVideoAdapter
import com.example.piece.vm.JokesViewModel
import com.kongzue.dialogx.dialogs.TipDialog

/**
 * 作品
 */
class ZPVideoFragment : BaseFragment<FragmentZPVideoBinding>() {

    override fun getContentRes(): Int = R.layout.fragment_z_p_video

    var type: Int = 0
    lateinit var targetUserId: String
    private val jokesViewModel: JokesViewModel by lazy {
        getViewModel<JokesViewModel>()
    }

    private val adapter: ZPVideoAdapter by lazy {
        val adapter = ZPVideoAdapter()
        mBinding.rv.adapter = adapter
        adapter
    }
    companion object {

        @JvmStatic
        fun newInstance(targetUserId: String, type: Int) =
            ZPVideoFragment().apply {
                arguments = Bundle().apply {
                    putString("targetUserId", targetUserId)
                    putInt("type", type)
                }
            }
    }
    var page: Int = 1

    override fun initData(view: View, savedInstanceState: Bundle?) {
        super.initData(view, savedInstanceState)
        type = arguments?.getInt("type", 0)!!
        targetUserId = arguments?.getString("targetUserId", "").toString()
        adapter.setEmptyView(R.layout.layout_empty)

        mBinding.srl.setOnRefreshListener {
            page = 1
            getHomeLatest()
        }
        adapter.loadMoreModule.isAutoLoadMore = true
        adapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = true
        getHomeLatest()
        adapter.loadMoreModule.setOnLoadMoreListener {
            getHomeLatest()
        }
        adapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(
                adapter: BaseQuickAdapter<*, *>,
                view: View,
                position: Int,
            ) {
                var ids = ""

                for ((index, id) in adapter.data.withIndex()) {
                    if (index == adapter.data.size - 1) {
                        ids += (id as JokesVideoLikeListEntity).jokeId

                    } else {
                        ids += (id as JokesVideoLikeListEntity).jokeId + ","

                    }
                }
                navigationTo(
                    RoutePath.PLAY_VIDEO, mutableMapOf(
                        Key.DataKey.IDS to ids,
                        Key.DataKey.IDS_POSITION to position)
                )
            }

        })

    }

    //获取最新
    private fun getHomeLatest() {
        when (type) {

            2 -> { //个人信息页  作品 视频
                jokesViewModel.jokesVideoList(null,targetUserId, page.toString()) {
                    dataSucc(it)

                } onError {
                    TipDialog.show(it)
                    adapter.loadMoreModule.loadMoreFail()
                    mBinding.srl.isRefreshing = false

                }
            }
            4 -> { //个人信息页  喜欢 视频
                 jokesViewModel.jokesVideoLikeList(null,targetUserId, page.toString()) {
                     dataSucc(it)

                 } onError {
                     TipDialog.show(it)
                     adapter.loadMoreModule.loadMoreFail()
                     mBinding.srl.isRefreshing = false

                 }
            }
        }

    }

    private fun dataSucc(it: Rest<List<JokesVideoLikeListEntity>>) {
        adapter.loadMoreModule.loadMoreComplete()

        if (it.code == 200) {
            page++
            if (it.data.isNullOrEmpty()) {
                adapter.loadMoreModule.isEnableLoadMore = false
                adapter.loadMoreModule.isAutoLoadMore = false
                //  return@userCollectList
            }
            if (page == 2) {
                adapter.setList(it.data)

            } else {
                adapter.addData(it.data)

            }

        } else {
            TipDialog.show(it.msg)
            adapter.loadMoreModule.loadMoreFail()

        }
        mBinding.srl.isRefreshing = false
    }
}